import BaseOffice from "@/plugins/office/extend/base-office";
import globalData from "@/plugins/data";
import globalMethods from "@/plugins/methods";

/*wps加载项*/
export default class WpsOffice extends BaseOffice {


  async addLock({userId}) {
    let locks = window.callBackendMethod("colManager", "getSummaryLock", globalData.summaryId);
    if (locks && locks.length) {
      let some = locks.some(lock => lock.owner === userId);
      if (!some) {
        //TODO 代码不对
        //let res = window.callBackendMethod("colManager", "readLock", globalData.summaryId);
        globalMethods.warning({
          title: globalMethods.i18n('common.system.hint.label'),
          content: globalMethods.i18n('collaboration.mpc.pc.edit.msg')
        });
        return false;
      }
    } else {
      const res = window.callBackendMethod("colManager", "writeLock", globalData.summaryId);
      //获取锁失败且锁不属于自己
      if (res?.success !== true && res?.lockInfo?.owner !== userId) {
        globalMethods.warning({
          title: globalMethods.i18n('common.system.hint.label'),
          content: res.msg,
        });
        return false;
      }
    }
    return true;
  }

  /**
   * 编辑功能
   * @param bodyType
   * @param userId
   * @param zwIframe
   * @param canSign
   * @return {Promise<void>}
   */
  async editOffice({bodyType, userId, zwIframe, canSign}) {
    let lock = await this.addLock({userId});
    if (!lock){
      return;
    }

    zwIframe.document.getElementById('viewState').value = '1';
    let transParams = this.getTransParams(zwIframe);
    if (transParams) {
     await this.editOfficeTrans({zwIframe,transParams,userId})
      // return;
    }
    //防止重复盖章
    if (await zwIframe.OfficeAPI.hasSpecialSignature()||await zwIframe.OfficeAPI.countSignatures(true)){
      await zwIframe.OfficeAPI.updateOfficeState("0,0");
      zwIframe.OfficeAPI.setEditType("0,0");
      globalMethods.warning({
        title: globalMethods.i18n('common.system.hint.label'),
        content: globalMethods.i18n('collaboration.alert.CantModifyBecauseOfIsignature'),
      });
      //document.getElementById("modifyFlag").value="0";
      zwIframe.WpsConstent.setWpsMini();
      return;
    }
    let modifySuccess = await zwIframe.OfficeAPI.modifyContent(canSign);
    if(modifySuccess){
      await zwIframe.OfficeAPI.fullSize(true);
      //设置正文为编辑状态 wps不在这里保存修改正文日志
      zwIframe.document.getElementById('viewState').value = '2';
    }
  }
  

  /**
   * 转换开启时候的编辑方式
   * @param zwIframe
   * @param transParams
   * @param userId
   * @return {Promise<void>}
   */

  async editOfficeTrans({zwIframe, transParams, userId}) {
    const {fileId,createDate,officeOcxUploadMax,fileType,fileName}=transParams;
    //canEditContent:'true'
    await zwIframe.OfficeAPI.showOffice({
      fileId,
      createDate,
      officeOcxUploadMax,
      fileType,
      fileName,
      handWriteCurrentUserId: userId,
      editType: "2,0",
      officecanPrint: true,
      officecanSaveLocal: true,
      //协同
      category: "1",
      showMode: 2, // kgApp模式，先隐藏显示
      wpsPopupFlag: "N", //使用静默模式不主动弹出
    })
  }
}

